<template>
  <a-modal
    :open="open"
    width="1200px"
    title="新增隔离登记"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    destroyOnClose
    @ok="onOk"
    @cancel="onCancel"
  >
    <div class="flex">
      <div class="w-1/2">
        <BaseTitle title="基础信息" />
        <div class="h-[600px] overflow-auto">
          <PatientInfo
            v-model:value="patientState"
            :patient-type="'防疫患者'"
            :show-divider-bot-info="false"
            :table-width="700"
            item-class="w-1/2"
            :search-params="{
              pyjm: patientState.brName,
              yyzt: '0'
            }"
          />
        </div>
      </div>
      <div class="w-1/2">
        <BaseTitle title="隔离信息" />
        <a-form class="form-style-flex" name="validate_other" :model="formState">
          <a-form-item label="病区名称" class="w-3/5">
            <SelectArea v-model:value="formState.wardId" disabled use-cache set-cache />
          </a-form-item>
          <a-form-item name="bedCode" label="床位号" class="w-3/5">
            <SelectTable
              v-model:value="formState.bedCode"
              type="床位"
              :placeholder="formState.wardId ? '请选择' : '请选择病区'"
              :disabled="!formState.wardId"
              :search-params="{ bqid: formState.wardId }"
              :row-disabled="(row) => row.status === 2 || row.status === 1"
              @select="onSelectWard"
            />
          </a-form-item>
          <a-form-item label="开始日期" class="w-3/5">
            <a-date-picker v-model:value="formState.startTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </a-form-item>
          <a-form-item label="结束日期" class="w-3/5">
            <a-date-picker v-model:value="formState.endTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </a-form-item>
          <a-form-item label="隔离原因" class="w-3/5">
            <SelectItem v-model:value="formState.isolateReason" select-first type="DT00301" placeholder="请选择" />
          </a-form-item>
          <a-form-item label="备注" class="w-full mb-3">
            <a-textarea v-model:value="formState.memo" :rows="8" placeholder="请输入" />
          </a-form-item>
        </a-form>
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { post } from '@/utils/request'
import PatientInfo from './PatientInfo'

const emit = defineEmits(['update:open', 'update:value', 'success'])

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const patientState = ref({})

const formState = ref({
  startTime: dayjs().format('YYYY-MM-DD'),
  endTime: dayjs().format('YYYY-MM-DD')
})
const rulesState = ref({})
const { isCompleted, resetFields } = useForm(formState, rulesState)

const onOk = async () => {
  if (!patientState.value.brName || !Number(patientState.value.brAge)) {
    message.error('请填写患者信息')
    return
  }
  // 保存患者信息
  const res = await post('/common/patient/savePatientInfo', { ...patientState.value })
  if (res.code !== 0) return message.error(res.msg)
  const { code, msg } = await post('/war/epidemicPrevention/doSaveEpidemicIsolate', { ...formState.value, brId: patientState.value.brId })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}

// 床位号
const onSelectWard = (select) => {
  formState.value.bedCode = select.bedCode
  formState.value.bedId = select.id
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData, startTime: dayjs().format('YYYY-MM-DD'), endTime: dayjs().format('YYYY-MM-DD'), wardId: 'BQ00000201' }
      patientState.value = { ...props.rowData }
    }
  }
)
</script>
<style lang="less" scoped></style>
